/*
As3 Performances Analyzer
Copyright (C) 2011 Ankama Studio

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Contact : seyraud@ankama.com
Project : http://code.google.com/p/as3-performances-analyzer
*/

package types
{
	import interfaces.IFrameActionContainer;
	import interfaces.ITimelineData;

	public class FrameAction implements IFrameActionContainer, ITimelineData
	{
		private static const INDENT : String = "  ";
		
		//:///////////////////////////////////////////////////////////////////
		//:// Privates variables
		//:///////////////////////////////////////////////////////////////////
		
		private var _id : uint;
		private var _start : uint;
		private var _end : uint;
		private var _duration : uint;
		private var _depth : uint;
		private var _name : String;
		private var _customInfo : String;
		private var _children : Vector.<ITimelineData> = new Vector.<ITimelineData>;
		private var _legend : String;
		private var _memoryStart : uint;
		private var _memoryEnd : uint;
		private var _memoryDiff : int;
		
		//:///////////////////////////////////////////////////////////////////
		//:// Getter / Setter
		//:///////////////////////////////////////////////////////////////////
		
		/** @inherited  */
		public function get name() : String {return _name;}/** @private */
		public function set name(value:String):void{
			_name = value;
			makeLegend();
		}
		
		/** @inherited  */
		public function get id():uint{return _id;}/** @private */
		public function set id(value:uint):void{_id = value;}
		
		/** @inherited */
		public function get start():uint{return _start;}/** @private */
		public function set start(value:uint):void{
			_start = value;
			_duration = _end - _start;
		}
		
		/** @inherited */
		public function get end():uint{return _end;}/** @private */
		public function set end(value:uint):void{
			_end = value;
			_duration = _end - _start;
		}
		
		/** @inherited */
		public function get startMemory():uint{return _memoryStart;}/** @private */
		public function set startMemory(value:uint):void{
			_memoryStart = value;
			_memoryDiff = _memoryEnd - _memoryStart;
		}
		
		/** @inherited */
		public function get endMemory():uint{return _memoryEnd;}/** @private */
		public function set endMemory(value:uint):void{
			_memoryEnd = value;
			_memoryDiff = _memoryEnd - _memoryStart;
		}
		
		public function get memoryDiff() : int{ return _memoryDiff;}
		
		
		/** @inherited */
		public function get duration():uint{return _duration;}
		
		/** @inherited */
		public function get depth():uint{return _depth;}/** @private */
		public function set depth(value:uint):void{_depth = value;}
		
		/** @inherited  */
		public function get customInfo() : String {return _customInfo;}/** @private */
		public function set customInfo(value:String):void{
			_customInfo = value;
			makeLegend();
		}
		
		/** Sub actions list */
		public function get children():Vector.<ITimelineData>{return _children;} /** @private */
		public function set children(value:Vector.<ITimelineData>):void {_children = value;}
		
		public function get legend() : String
		{
			return _legend;
		}
		
		//:///////////////////////////////////////////////////////////////////
		//:// Public var
		//:///////////////////////////////////////////////////////////////////
		
		/** Parent action */
		public var parent : FrameAction;
		
		/** How long children ran */
		public var childrenDuration : uint;
		
		/** Children memory */
		public var childrenMemory : int;
		
		//:///////////////////////////////////////////////////////////////////
		//:// Public function
		//:///////////////////////////////////////////////////////////////////
		
		public function toString(indent:String="") : String
		{
			var r : String = indent + name + ", during " + (end - start) + " ms, depth: " + depth + ", children : " + children.length +", start at " + start;
			var i : uint = 0;
			while(i < children.length)
			{
				r += "\n" + FrameAction(children[i]).toString(indent + INDENT);
				++i;
			}
			return r;
		}
		
		//:///////////////////////////////////////////////////////////////////
		//:// Private function
		//:///////////////////////////////////////////////////////////////////
		
		private function makeLegend() : void
		{
			var splitIndex : int = _name.indexOf("::");
			_legend = (splitIndex != -1 ? _name.substr(splitIndex + 2) : _name)
					+ (_memoryDiff ? (", mem: " + (_memoryDiff > 0 ? "+" : "") + Math.floor(_memoryDiff / 1024) + "ko ") : "")
					+ (_customInfo ? "\n" + _customInfo : ""); 
		}
	}
}